﻿/*
 *  Author: Louis A. Leskoviansky
 *  Company: Artemis Solutions Group (http://www.artemis-solutions.com)
 */


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Web;
using System.Web.Compilation;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AjaxControlFramework.Compilation
{
    public static class JsxScriptRegistry
    {
        //------// Properties \\--------------------------------------------\\
        private static Dictionary<string, JsxScript> _internalCollection = null;
        private static Dictionary<string, JsxScript> InternalCollection
        {
            get
            {
                if (_internalCollection == null)
                {
                    _internalCollection = new Dictionary<string, JsxScript>();
                    InitializeRegistry();
                }

                return _internalCollection;
            }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        private static void InitializeRegistry()
        {
            // Search for all types annotated with the CompiledJsxScript attribute.

            /*
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {

                
            }
            */
        }


        public static void Add(JsxScript script)
        {
            if (script == null || String.IsNullOrEmpty(script.FileName) || String.IsNullOrEmpty(script.ScriptName)) { return; }

            lock (InternalCollection)
            {
                InternalCollection.Add(script.ScriptName, script);
            }

            JsxScriptHandler.AddScriptHandler(script);
        }


        public static void Clear()
        {
            lock (_internalCollection)
            {
                InternalCollection.Clear();
            }
        }


        public static JsxScript GetScript(string name)
        {
            if (InternalCollection.ContainsKey(name))
            {
                return InternalCollection[name];
            }

            return null;
        }


        public static bool Contains(string name)
        {
            if (String.IsNullOrEmpty(name)) { return false; }

            return InternalCollection.ContainsKey(name);
        }


        public static void Remove(JsxScript script)
        {
            if (script == null || String.IsNullOrEmpty(script.ScriptName)) { return; }

            lock (InternalCollection)
            {
                InternalCollection.Remove(script.ScriptName);
            }
        }


        public static IEnumerator GetEnumerator()
        {
            return InternalCollection.GetEnumerator();
        }
        //------\\ Methods //-----------------------------------------------//
    }
}
